package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealSercive;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class SetmealSerciveImpl implements SetmealSercive {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        //1.封装分页参数
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        //2.调用分页查询方法
        Page<SetmealVO> page = setmealMapper.pageQuery(dto);

        //3.封装PageResult并返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     * @param dto
     */
    @Transactional //0.开启事务
    public void addSetmeal(SetmealDTO dto) {
        //新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.insert(setmeal);
        log.info("SetmealID={},",setmeal.getId());

        //新增套餐菜品关系
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        //调用mapper
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 修改套餐
     * @param dto
     */
    @Transactional //0.开启事务
    public void updateSetmeal(SetmealDTO dto) {
        //1.修改套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.update(setmeal);

        //2.修改套餐菜品关系表
        //2.1删除套餐菜品关系表中的套餐数据
            setmealDishMapper.deleteBySetmealId(dto.getId());
        //2.2重新插入套餐菜品关系表
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(dto.getId());
            });
            //向套餐菜品关系表插入n条数据
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }


    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional//0.开启事务
    public void deleteSetmeal(List<Long> ids) {
        //判断是否起售
        List<Integer> status= setmealMapper.selByids(ids);
        status.forEach(sta->{
            if (sta == StatusConstant.ENABLE){
                throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐数据
        setmealMapper.deleteSetmeal(ids);

        //删除套餐对应的菜品数据
        setmealDishMapper.deleteBySId(ids);
    }

    /**
     * 根据id查询套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO selById(Long id) {
        //1.根据id查询套餐数据
        SetmealVO setmealVO = setmealMapper.selByid(id);

        //2.根据id查询套餐对应的菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.selByid(id);

        //3.将查询到的数据封装成SetmealVO对象并返回
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 起售停售套餐
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询----用户端
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项---用户端
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
